#ifndef GGEODESICSPHERE_H
#define GGEODESICSPHERE_H

#include <GL/glew.h>
#include <QVector2D>
#include <QVector3D>
#include <QColor>
class QGLShaderProgram;
class GeodesicSphere
{
  public:
    GeodesicSphere();
    GeodesicSphere(const QVector3D& pos, float radius, float maxEdgeLength);
    ~GeodesicSphere();

    const QVector3D& pos() const;
    void setSize(float size);
    void setPos(const QVector3D& pos);
    void setShader(QGLShaderProgram *shader);
    void setColor(const QColor& color);
    void generateTextureCoords(bool generateTextureCoords);

    void create();
    void render();

  protected:
    void drawTriangle(const QVector3D &a,
                      const QVector3D &b,
                      const QVector3D &c);

    void subdivideThenRenderTriangle(const QVector3D &a,
                                     const QVector3D &b,
                                     const QVector3D &c);

    QVector3D midpointOnSphere(const QVector3D &a,
                               const QVector3D &b);

    QVector2D latLonFromPoint(const QVector3D& point) const;

    QVector3D mPosition;
    float mRadius;
    float mMaxEdgeLength;
    GLuint mDisplayListID;
    QColor mColor;
    QGLShaderProgram *mShader;
    bool mGenerateTextureCoords;
};

#endif // GGEODESICSPHERE_H
